package com.ymh.git.thread.juc;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Exchanger;

/**
 * Created by aivin on 2018/11/1.
 * 演示 Exchange的用法
 */
public class UseExchange {

    private static final Exchanger<Set<String>> exchanger = new Exchanger<>();


    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Set<String> setA = new HashSet<>();//存放数据的容器
                try {
                    /**
                     * 添加数据 set.add(...)
                     */
                    setA = exchanger.exchange(setA);
                    /**
                     * 处理交换后的数据
                     */
                } catch (Exception e) {

                }

            }
        }, "Thread-A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Set<String> setB = new HashSet<>();//存放数据的容器
                try {
                    /**
                     * 添加数据 set.add(...)
                     */
                    setB = exchanger.exchange(setB);
                    /**
                     * 处理交换后的数据
                     */
                } catch (Exception e) {

                }

            }
        }, "Thread-B").start();


    }
}
